package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.OrderDetail;
import com.itheima.domain.Orders;
import com.itheima.domain.ShoppingCart;
import com.itheima.dto.OrdersDto;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("orders:{}", orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        log.info("page:{},pageSize:{}", page, pageSize);
        Page userPage = ordersService.userPage(page, pageSize);
        return R.success(userPage);
    }


    @GetMapping("/page")
    public R<Page> ordersPage(int page, int pageSize, Long number, @DateTimeFormat String beginTime, @DateTimeFormat String endTime) {
        log.info("beginTime:{}", beginTime);
        log.info("endTime:{}", endTime);
        log.info("page:{},pageSize:{},number:{}", page, pageSize, number);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(number != null, Orders::getNumber, number);
        wrapper.between(beginTime != null && endTime != null, Orders::getCheckoutTime, beginTime, endTime);
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, wrapper);
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        List<Orders> ordersList = ordersPage.getRecords();
        List<OrdersDto> ordersDtoList = ordersList.stream().map(new Function<Orders, OrdersDto>() {
            @Override
            public OrdersDto apply(Orders orders) {
                OrdersDto ordersDto = new OrdersDto();
                BeanUtils.copyProperties(orders, ordersDto);
                Long ordersId = orders.getId();
                LambdaQueryWrapper<OrderDetail> orderDetailWrapper = new LambdaQueryWrapper<>();
                orderDetailWrapper.eq(OrderDetail::getOrderId, ordersId);
                List<OrderDetail> orderDetails = orderDetailService.list(orderDetailWrapper);
                ordersDto.setOrderDetails(orderDetails);
                return ordersDto;
            }
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders) {
        log.info("orders:{}", orders);
        ordersService.updateById(orders);
        return R.success("修改成功");
    }

    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders) {
        log.info("orders:{}", orders);
//        ordersService.updateById(orders);
        Long ordersId = orders.getId();
        //查询订单中的菜品
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderDetail::getOrderId, ordersId);
        List<OrderDetail> orderDetails = orderDetailService.list(wrapper);
        List<ShoppingCart> shoppingCarts = orderDetails.stream().map(new Function<OrderDetail, ShoppingCart>() {
            @Override
            public ShoppingCart apply(OrderDetail orderDetail) {
                ShoppingCart shoppingCart = new ShoppingCart();
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setCreateTime(LocalDateTime.now());
//                shoppingCart.setName(orderDetail.getName());
//                shoppingCart.setImage(orderDetail.getImage());
//                shoppingCart.setDishId(orderDetail.getDishId());
//                shoppingCart.setSetmealId(orderDetail.getSetmealId());
//                shoppingCart.setDishFlavor(orderDetail.getDishFlavor());
//                shoppingCart.setNumber(orderDetail.getNumber());
//                shoppingCart.setAmount(orderDetail.getAmount());
                BeanUtils.copyProperties(orderDetail, shoppingCart);
                return shoppingCart;
            }
        }).collect(Collectors.toList());
        shoppingCartService.saveBatch(shoppingCarts);
        return R.success("再来一单成功");
    }
}
